//
// Created by 常磊 on 2022/4/5.
//

import Foundation
import Alamofire

// MARK: -

extension DioRequest {
    // MARK: Helper Types
    
    public typealias ValidationResult = Request.ValidationResult
}

extension DioDataRequest {
    public typealias Validation = DataRequest.Validation
    
    @discardableResult
    public func validate<S: Sequence>(statusCode acceptableStatusCodes: S) -> Self where S.Iterator.Element == Int {
        dataCore.validate(statusCode: acceptableStatusCodes)
        return self
    }
    
    @discardableResult
    public func validate<S: Sequence>(contentType acceptableContentTypes: @escaping @autoclosure () -> S) -> Self where S.Iterator.Element == String {
        dataCore.validate(contentType: acceptableContentTypes())
        return self
    }
    
    @discardableResult
    public func validate() -> Self {
        dataCore.validate()
        return self
    }
}

extension DioDataStreamRequest {
    public typealias Validation = (_ request: URLRequest?, _ response: HTTPURLResponse) -> ValidationResult
    
    @discardableResult
    public func validate<S: Sequence>(statusCode acceptableStatusCodes: S) -> Self where S.Iterator.Element == Int {
        dataCore.validate(statusCode: acceptableStatusCodes)
        return self
    }
    
    @discardableResult
    public func validate<S: Sequence>(contentType acceptableContentTypes: @escaping @autoclosure () -> S) -> Self where S.Iterator.Element == String {
        dataCore.validate(contentType: acceptableContentTypes())
        return self
    }
    
    @discardableResult
    public func validate() -> Self {
        dataCore.validate()
        return self
    }
}

// MARK: -

extension DioDownloadRequest {
    public typealias Validation = DownloadRequest.Validation
    
    @discardableResult
    public func validate<S: Sequence>(statusCode acceptableStatusCodes: S) -> Self where S.Iterator.Element == Int {
        dataCore.validate(statusCode: acceptableStatusCodes)
        return self
    }
    
    @discardableResult
    public func validate<S: Sequence>(contentType acceptableContentTypes: @escaping @autoclosure () -> S) -> Self where S.Iterator.Element == String {
        dataCore.validate(contentType: acceptableContentTypes())
        return self
    }
    
    @discardableResult
    public func validate() -> Self {
        dataCore.validate()
        return self
    }
}
